<?php
class NoteRPCHelper {
	static $instance = null;
	public static $dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
	/**
	 * 
	 * @return ParserHelper
	 */
	public static function get(){
		if(!is_object(self::$instance)){
			self::$instance = new NoteRPCHelper();
		}
		return self::$instance;
	}
	private function log($messages, $level='info'){
		Yii::getLogger()->log($messages, $level, 'epub.pickup');
	}
	public static function syncNotes($sync){
		//update to server
		foreach ($sync['local_notes'] as $note){
				//update by serverid
				if(isset($note['id'])){
					$current_note=Post::model()->findByPk($note['id']);
					if(isset($current_note)){
						$current_note->update_time=$note['date_modified'];
						$current_note->title=$note['title'];
						$current_note->content=$note['body'];
						$current_note->local_id=$note['local_id'];
						$current_note->save();
					}
				}else{//insert new note to server
						$newnote= new Post;
						//$newnote->update_time=$note['date_modified'];
						$newnote->title=$note['title'];
						$newnote->content=$note['body'];
						$newnote->local_id=$note['local_id'];
						$newnote->status=1;
						$newnote->isNewRecord=true;
						$newnote->save();
				}
		}

		//get data from server not sync --> sync to client
		$last_sync_log=SyncLog::model()->getLastestSync();
		$last_sync=null;
		if(isset($last_sync_log)){
			$last_sync=$last_sync_log->sync_time;
		}
		$criteria=new CDbCriteria;
		if(isset($last_sync)){
			$criteria->condition = '
			(
			      author_id ='. Yii::app()->user->id.'
			 )
			 AND
			 (
			 	   update_time > '. $last_sync.'
			 )
			 ';
			}
		else{
			$criteria->condition = '
			(
			      author_id ='. Yii::app()->user->id.'
			 )';
		}
		$criteria->select='id, title, content, tags, status, create_time, update_time, author_id';
		$criteria->order = 'id DESC';
		$notes=Post::model()->findAll($criteria);
		$result='';
		foreach ($notes as $note){
		//echo $note->toJson();
			if($result==''){
				$result=NoteRPCHelper::toJson($note);
			}else{
				$result=NoteRPCHelper::toJson($note).','.$result;	
			}
		}
		return '{"method": "'.$sync['method'].'", "data": {"notes": ['.$result.']}}'; 
	}
	
	public static function toJson($note){
		$jnote= array();
		$jnote["id"]= $note->id;
		$jnote["title"]=  $note->title;
		$jnote["content"]=  $note->content;
		$jnote["tags"]=  $note->tags;
		$jnote["status"]= $note->status;
		$jnote["create_time"]= $note->create_time;
		$jnote["update_time"]=  $note->update_time;
		$jnote["author_id"]=  $note->author_id;
		return json_encode($jnote);
	}
	
	public static function deviceRegister($sync){
		return '{"method": "'.$sync['method'].'", "data": {"notes": null}}';
	}
	public static function updateSyncLogs($sync){
		if(!isset($sync)) return;
		$method=null;
		$client_device_id=null;
		//$local_id=null;
		foreach($sync['calls'] as $call)
		{
			if(!isset($method)){
				$method=$call['method'];
			}else{
				$method=$method.','.$call['method'];
			}
			$client_device_id=$call['client_device_id'];
		}
		$synclog=new SyncLog();
		$synclog->method=$method;
		$synclog->client_device_id=$client_device_id;
		//$synclog->local_id=$local_id;
		$synclog->save();
	}
	
	
}
	?>